<?php 
include './Db.class.php';

$pdo=Db::getDB();

function response($error=0,$msg='',$data=[]){
    echo  json_encode(['error'=>$error,'msg'=>$msg,'data'=>$data],256);
    exit;
}

/*添加*/
if(isset($_REQUEST['type'])&&$_REQUEST['type']=='add'){
    if(empty($_REQUEST['username'])){
      response(1,'用户名不能为空!');
    }else{
      $username=$_REQUEST['username'];
    }
    if(empty($_REQUEST['mobile'])){
      response(1,'手机号不能为空!');
    }else{
      $mobile=$_REQUEST['mobile'];
    }
    if(!preg_match('/^1[3|4|5|7|8]\d{9}$/',$mobile)){
      response(1,'手机号格式不正确!');
    }
    if(empty($_REQUEST['contactType'])){
      response(1,'课程类型不能为空!');
    }else{
      $contactType=$_REQUEST['contactType'];
    }

    $sql = "SELECT * FROM `t_deruimu_user` WHERE `username` = :username and `mobile` = :mobile ";  
    $stmt = $pdo->prepare($sql);  
    $stmt->execute(array(':username'=>$username,':mobile'=>$mobile));
    $user=[];  
    while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)){     
        $user[]=$row;
    }
    if(!empty($user)){
      response(1,'用户信息已提交!');
    }
    $phpCookie=isset($_COOKIE['deruimuClient'])?$_COOKIE['deruimuClient']:'';
    $contactType=isset($_REQUEST['contactType'])?$_REQUEST['contactType']:1;
    $sql = "INSERT INTO `t_deruimu_user` (`createTime`,`username` ,`mobile`,`contactType`,`phpCookie`)VALUES (:createTime,:username, :mobile,:contactType, :phpCookie)";  
    $stmt = $pdo->prepare($sql);  
    $stmt->execute(array(':createTime'=>time(),':username'=>$username,':mobile'=>$mobile,':contactType'=>$contactType,':phpCookie'=>$phpCookie));  
    if($pdo->lastinsertid()){
        response(0,'用户信息提交成功!');
    }else{
        response(1,'用户信息提交失败!');
    }
}

/*设置cookie*/
if(isset($_REQUEST['type'])&&$_REQUEST['type']=='setcookie'){
    if(!isset($_REQUEST['jsCookie'])){
        response(1,'无效请求!');
    }else{
        $jsCookie = $_REQUEST['jsCookie'];  
    }
    if (!empty($_COOKIE['deruimuClient'])){
            $name=$_COOKIE['deruimuClient'];
    }else{
          $name=md5(time().uniqid());
          setcookie("deruimuClient",$name,time()+30*24*3600);
    }

    $sql = "SELECT * FROM `t_deruimu_cookie` WHERE `jsCookie` = :jsCookie";  
    $stmt = $pdo->prepare($sql);  
    $stmt->execute(array(':jsCookie'=>$jsCookie));
    $cookie=[];  
    while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)){     
        $cookie[]=$row;
    }
    if(empty($cookie)){
        $sql = "INSERT INTO `t_deruimu_cookie` (`createTime` ,`phpCookie`,`jsCookie`)VALUES (:createTime,:phpCookie, :jsCookie)";  
        $stmt = $pdo->prepare($sql);  
        $stmt->execute(array(':createTime'=>time(),':phpCookie'=>$name,':jsCookie'=>$jsCookie));  
        if($pdo->lastinsertid()){
            response(0,'操作成功!');
        }else{
            response(1,'操作失败!');
        }
    }else{
        $sql = "UPDATE `t_deruimu_cookie` SET `updateTime`=:updateTime WHERE `jsCookie`=:jsCookie";  
        $stmt = $pdo->prepare($sql);  
        $stmt->execute(array(':jsCookie'=>$jsCookie, ':updateTime'=>time()));  
        if($stmt->rowCount()){
            response(0,'操作成功!');
        }else{
            response(1,'操作失败!');
        } 
    } 
}


  response(1,'无效请求!');

?>